package com.ewhl.mark.nss.wxapi;

import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

import com.android.volley.VolleyError;
import com.ewhl.mark.nss.CompleteRegisterActivity;
import com.ewhl.mark.nss.MainActivity;
import com.ewhl.mark.nss.net.LoginBean;
import com.ewhl.mark.nss.net.NetValues;
import com.ewhl.mark.nss.net.WxInfo;
import com.ewhl.mark.nss.net.WxUserInfo;
import com.ewhl.mark.nss.utils.CommonUtils;
import com.ewhl.mark.nss.utils.EventData;
import com.ewhl.mark.nss.utils.SPUtils;
import com.ewhl.mark.nss.volley.BaseVO;
import com.ewhl.mark.nss.volley.MyErrorListener;
import com.ewhl.mark.nss.volley.MyReponseListener;
import com.tencent.mm.opensdk.modelbase.BaseResp;
import com.tencent.mm.opensdk.modelmsg.SendAuth;


import org.greenrobot.eventbus.EventBus;

import cn.sharesdk.wechat.utils.WXAppExtendObject;
import cn.sharesdk.wechat.utils.WXMediaMessage;
import cn.sharesdk.wechat.utils.WechatHandlerActivity;

public class WXEntryActivity extends WechatHandlerActivity {

    NetValues netValues;
    Dialog dialog;
    String url="http://www.nongshengsheng.com/index.php/Mobile/Index/index";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.e("WXEntryActivity", "onCreate");
        netValues = NetValues.getInstance(this);
        handleIntent(getIntent());
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        Log.e("WXEntryActivity", "onNewIntent");
        handleIntent(intent);
    }

    private void handleIntent(Intent intent) {
        SendAuth.Resp resp = new SendAuth.Resp(intent.getExtras());
        Log.e("WXEntryActivity", "handleIntent" + resp.errCode);
        Log.e("WXEntryActivity", "handleIntent" + resp.code);
        if (resp.errCode == BaseResp.ErrCode.ERR_OK) {

            netValues.getwx_OpenID(resp.code, new MyReponseListener() {
                @Override
                public void onResponse(final BaseVO arg0) {
                    final WxInfo wxInfo = (WxInfo) arg0;
                    if (wxInfo != null) {

                        if(wxInfo.getOpenid()!=null){
                            SPUtils.put(WXEntryActivity.this, "open_id", wxInfo.getOpenid());
                        }
                        Log.d("wxinfo", "openid:" + wxInfo.getOpenid());
                        Log.d("wxinfo", "info:" + wxInfo.toString());
                        Log.d("wxinfo", "actoken:" + wxInfo.getAccess_token());
                        Toast.makeText(WXEntryActivity.this, "授权成功", Toast.LENGTH_SHORT).show();
                        if(wxInfo.getOpenid()!=null){
                            SPUtils.put(WXEntryActivity.this,"wx_openid",wxInfo.getOpenid());
                        }
                        netValues.checkReg(wxInfo.getOpenid(), "weixin", new MyReponseListener() {
                            @Override
                            public void onResponse(BaseVO t) {
                                LoginBean checkLoginBean = (LoginBean) t;
                                if (checkLoginBean.status==200) {
                                    SPUtils.put(WXEntryActivity.this, "key", checkLoginBean.getResult());
                                    Intent it=new Intent(WXEntryActivity.this,MainActivity.class);
                                    it.putExtra("load_url",url+"/key/"+checkLoginBean.result);
                                    EventBus.getDefault().post(new EventData(EventData.EVENT_TYPE_PERSON_WX,checkLoginBean.result));
                                    it.setFlags(  Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
                                    startActivity(it);

                                } else  {
                                    Toast.makeText(WXEntryActivity.this, ""+checkLoginBean.getMsg(), Toast.LENGTH_SHORT).show();

                                }
//                                finish();
                            }
                        }, new MyErrorListener() {
                            @Override
                            public void onErrorResponse(VolleyError error) {
                                super.onErrorResponse(error);
                            }
                        });


                    }
                }


            }, new MyErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    super.onErrorResponse(error);
                }
            });
        } else {
            Toast.makeText(this, " 授权失败", Toast.LENGTH_SHORT).show();
            finish();
        }
    }

    private void getwxinfo(final WxInfo wxInfo) {
        netValues.getWx_info(wxInfo.getAccess_token(), wxInfo.getOpenid(), new MyReponseListener() {
            @Override
            public void onResponse(BaseVO arg0) {
                try {
                    WxUserInfo wxUserInfo = (WxUserInfo) arg0;
                    if (wxUserInfo != null) {
                        Intent it=new Intent(WXEntryActivity.this, CompleteRegisterActivity.class);
                        it.putExtra("header",wxUserInfo.getHeadimgurl());
                        it.putExtra("nickname", new String(wxUserInfo.getNickname().getBytes("ISO-8859-1"),"UTF-8"));
                        it.putExtra("openid",wxUserInfo.getOpenid());
                        it.putExtra("sex",wxUserInfo.getSex());
                        it.putExtra("oauth","weixin");
                        startActivity(it);
                        finish();
                    }
                } catch (Exception e) {
                    e.printStackTrace();

                    finish();
                }
            }
        }, new MyErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                super.onErrorResponse(error);
            }
        });
    }



    @Override
    public void onGetMessageFromWXReq(WXMediaMessage msg) {
        Intent ilself=getPackageManager().getLaunchIntentForPackage(getPackageName());
        startActivity(ilself);
    }

    @Override
    public void onShowMessageFromWXReq(WXMediaMessage msg) {
      if(msg!=null&&msg.mediaObject!=null&&(msg.mediaObject instanceof WXAppExtendObject)){
         WXAppExtendObject wxobj=(WXAppExtendObject) msg.mediaObject;

      }
    }


    //    @Override
//    public void onReq(BaseReq baseReq) {
//        finish();
//    }
//
//    @Override
//    public void onResp(BaseResp baseResp) {
//        String result = "";
//        switch(baseResp.errCode) {


//            case BaseResp.ErrCode.ERR_OK:
//                result ="发送成功";
//                Toast.makeText(this, result, Toast.LENGTH_LONG).show();
//                finish();
//                break;
//            case BaseResp.ErrCode.ERR_USER_CANCEL:
//                result = "发送取消";
//                Toast.makeText(this, result, Toast.LENGTH_LONG).show();
//                finish();
//                break;
//            case BaseResp.ErrCode.ERR_AUTH_DENIED:
//                result = "发送被拒绝";
//                Toast.makeText(this, result, Toast.LENGTH_LONG).show();
//                finish();
//                break;
//            default:
//                result = "发送返回";
//                Toast.makeText(this, result, Toast.LENGTH_LONG).show();
//                finish();
//                break;
//        }
//    }
}